package org.jeecg.modules.submodules.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.core.instrument.util.DoubleFormat;
import io.netty.util.internal.StringUtil;
import org.jeecg.modules.submodules.entity.BigOrders;
import org.jeecg.modules.submodules.entity.vo.*;
import org.jeecg.modules.submodules.mapper.BigOrdersMapper;
import org.jeecg.modules.submodules.service.IBigOrdersService;
import org.jeecg.modules.system.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

/**
 * @Description: 订单组
 * @Author: jeecg-boot
 * @Date:   2020-07-17
 * @Version: V1.0
 */
@Service
public class BigOrdersServiceImpl extends ServiceImpl<BigOrdersMapper, BigOrders> implements IBigOrdersService {
    @Autowired
    BigOrdersMapper mapper;
    @Override
    public List<BigOrdersDetailVo> getOrdersListByBigOrdersId(String id) {
        return mapper.getOrdersListByBigOrdersId(id);
    }

    @Override
    public Page<BigOrdersPageVo> getBigOrdersPageVoList(BigOrdersPageVo bigOrdersPageVo, Page page) {
        List<BigOrdersPageVo> list = mapper.getBigOrdersPageVoList(bigOrdersPageVo);
        for (BigOrdersPageVo i : list){
            switch (i.getBigOrdersStatus()){
                case "1":
                    i.setBigOrdersStatusDicText("订单创建成功");
                    break;
                case "2":
                    i.setBigOrdersStatusDicText("分派司机成功");
                    break;
                case "3":
                    i.setBigOrdersStatusDicText("司机正在装货");
                    break;
                case "4":
                    i.setBigOrdersStatusDicText("司机已经出发");
                    break;
                case "5":
                    i.setBigOrdersStatusDicText("司机已经到达");
                    break;
                case "6":
                    i.setBigOrdersStatusDicText("订单已结束");
                    break;
            }
        }
        if (list.size()<page.getSize()){
            page.setRecords(list);
            page.setTotal(list.size());
            return page;
        }
        page.setRecords(PageUtil.pageBySubList(list,(int)page.getSize(),(int)page.getCurrent()));
        page.setTotal(list.size());
        return page;
    }

    @Override
    public List<GoodsWeightRecordVo> groupEndAndOrdersId(String bigOrdersId) {
        return mapper.groupEndAndOrdersId(bigOrdersId);
    }

    @Override
    public String getTimeIDByEndIdAndBiOrdersId(String endId, String bigOrdersId) {
        return mapper.getTimeIDByEndIdAndBiOrdersId(endId,bigOrdersId);
    }

    @Override
    public List<CarStatisticsVo> getCarReceivingOrders(CarStatisticsVo vo) {
        List<CarStatisticsVo> list = mapper.getCarReceivingOrders(vo);
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        if (vo.getFlag()!=null&& (StringUtil.isNullOrEmpty(vo.getStartDate())&&StringUtil.isNullOrEmpty(vo.getEndDate()))){
            if (vo.getFlag()==1){
                //获取一周平均数
                for (CarStatisticsVo car : list){
                    Float f = new Float(car.getCounts());
                    car.setAvgOrders(decimalFormat.format(f/7));
                }
            }else if (vo.getFlag()==2){
                //获取本月平均数
                Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
                int day=aCalendar.getActualMaximum(Calendar.DATE);
                for (CarStatisticsVo car : list){
                    Float f = new Float(car.getCounts());
                    car.setAvgOrders(decimalFormat.format(f/day));
                }
            }else if (vo.getFlag()==3){
                //获取一年每天平均数
                for (CarStatisticsVo car : list){
                    Float f = new Float(car.getCounts());
                    car.setAvgOrders(decimalFormat.format(f/365));
                }
            }
        }
        return list;
    }

    @Override
    public List<GoodsStatisticsVo> getAllGoodsWeight(GoodsStatisticsVo vo) {
        return  mapper.getAllGoodsWeight(vo);
    }

    @Override
    public List<CarStatisticsVo> getCarReceivingOrdersAvg(CarStatisticsVo vo)  {

        return null;
    }

    @Override
    public String getZhouzhuan() {
        String zhouzhuan = mapper.getZhouzhuan();
        if (!StringUtil.isNullOrEmpty(zhouzhuan)){
            DecimalFormat df = new DecimalFormat("0.00%");
            String r = df.format(new BigDecimal(zhouzhuan));
            return r;
        }else {
            return "0.00%";
        }

    }

    @Override
    public List<AvgWeight> getAvgWeight(String carId,String startDate ,String endDate) {
        return mapper.getAvgWeight(carId,startDate,endDate);
    }

    @Override
    public List<MyExecuteOrdersVo> getMyExecuteOrders(String userId) {
        return mapper.getMyExecuteOrders(userId);
    }

    @Override
    public List<EndCheckVo> getEndCheckListVo(String bigOrdersId) {
        return mapper.getEndCheckListVo(bigOrdersId);
    }

    @Override
    public BigOrdersDetailVo getDetailOrdersId(String ordersId) {
        return mapper.getDetailOrdersId(ordersId);
    }

    @Override
    public List<BigOrdersPageVo> captainSearchBigOrders(String username) {
        return mapper.captainSearchBigOrders(username);
    }

    @Override
    public List<BigOrdersExcelVo> getExcelByBigOrders(String startDate , String endDate) {
        return mapper.getExcelByBigOrders(startDate,endDate);
    }

    @Override
    public List<BigOrders> getNowDayBigOrdersCount() {
        return mapper.getNowDayBigOrdersCount();
    }

    @Override
    public List<String> getCaptainCid() {
        return mapper.getCaptainCid();
    }
}
